package com.xtaller.common.convert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class L {

    public static String getAttrs(Object list, String attr){
        List<String> attrs = new ArrayList<String>();
        List<JSONObject> objects = J.o2l(list);
        for(JSONObject object:objects){
            attrs.add(object.getString(attr));
        }
        return String.join(",",attrs);
    }
    public static String getAttrs(List<JSONObject> list, String attr){
        List<String> attrs = new ArrayList<String>();
        for(JSONObject object:list){
            attrs.add(object.getString(attr));
        }
        return String.join(",",attrs);
    }
    public static String[] getAttrs2Arr(List<JSONObject> list, String attr){
        String attrs = getAttrs(list, attr);
        return removeRepeat(attrs.split(","));
    }

    public static List<JSONObject> f2o(List<JSONObject> list, String attr){
        for(JSONObject o:list){
            o.put(attr, new JSONObject());
        }
        return list;
    }
    public static List<JSONObject> f2l(List<JSONObject> list, String attr){
        for(JSONObject o:list){
            o.put(attr, new ArrayList<>());
        }
        return list;
    }

    // 创建一个空属性
    public static List<JSONObject> a2l(List<JSONObject> list, Object object, String... attrs){
        for(JSONObject o:list){
            for(String attr:attrs) {
                o.put(attr, object);
            }
        }
        return list;
    }
    public static JSONObject a2o(JSONObject o, Object object, String... attrs){
        for(String attr:attrs) {
            o.put(attr, object);
        }
        return o;
    }
    // 更换属性 / 创建对等属性
    public static List<JSONObject> u2l(List<JSONObject> list, String old, String news){
        for(JSONObject o:list){
            o.put(news, o.getString(old));
        }
        return list;
    }

    // 递归生成树
    public static JSONArray list2Tree(List<JSONObject> objects,
                                      String parentId){
        JSONArray children = new JSONArray();
        for(JSONObject object:objects){
            String id = object.getString("id");
            String pid = object.getString("parentId");
            if(parentId.equals(pid)){
                JSONArray node = list2Tree(objects, id);
                object.put("children", node);
                children.add(object);
            }
        }
        return children;
    }
    public static JSONArray list2Tree(List<JSONObject> objects,
                                      String parentId,
                                      int weight){
        JSONArray children = new JSONArray();
        for(JSONObject object:objects){
            String id = object.getString("id");
            String pid = object.getString("parentId");
            if(parentId.equals(pid)){
                object.put("weight", weight);
                // 20180607 格式化数据符合easyui的tree组件数据
                object.put("text", object.getString("name"));
                JSONArray node = list2Tree(objects, id, (weight + 1));
                object.put("children", node);
                children.add(object);
            }
        }
        return children;
    }

    // 去掉数组中的重复值
    public static String[] removeRepeat(String[] data){
        List<String> list = new ArrayList<>();
        boolean flag;
        for(String d:data){
            flag = false;
            for(int i=0;i<list.size();i++){
                if(V.isEqual(d, list.get(i))){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                list.add(d);
            }
        }
        return (String[])list.toArray(new String[list.size()]);
    }
}
